#!/usr/local/python36/bin/python3.6
#coding:utf-8
from config import *

import redis
from dbread import *
from operation import *
from Email import *
import time
sendtime=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
#发送微信给运维人员

#在zabbix 可以找到告警收敛的动作ID（actionid）
actionid=actionid
#连接redis，并读取所有事件id
r = redis.StrictRedis(host=redis_ip, port=redis_port)

subjectlist_tmp=r.keys()
subjectlist = []

for i in subjectlist_tmp:
    subjectlist.append(str(i.decode("utf-8")))

# print("---------------------------------")
# print(subjectlist)

#获取以后清空所有的key
for i in subjectlist:
    r.delete(i)
r.flushdb()
#获取原始数据并存入数据库

for action_id in actionid:
    originallist = []
    print('-----------------------------action_id on  %s-----------------------------' %(action_id))

    for subject in subjectlist:
        print("---------subject %s**%s------------" % (subject,action_id))
        a=alerts_eventid(str(action_id),subject)
        if a == "x1":
            pass
        else:
            originallist.append(a)
# print("-------------------------")
# print(originallist)
    print("----------------------%s发送告警信息---------------------------" %(action_id))
    # print(originallist)
    if len(originallist) != 0:
        problem=mergeproblem(originallist)
        normal=mergenormal(originallist)
        # print("----------------")
        # print(problem)
        # print(normal)
        #发送告警信息

        messagelist=compressproblem(problem)["messagelist"]
        print("------------messagelist----------------")
        print(messagelist)
        Subject=compressproblem(problem)["Subject"]
        user_index = actionid.index(action_id)
        slack_webhook_index = user_index
        weixin_webhook_index = user_index

        print(Subject)
        if len(messagelist) != 0:
            for content in  messagelist:
                Subject_tmp = content["title"]
                if email_status == 1:
                    user_tmp = 'send_to_users_%s' % (user_index)
                    users = locals()[user_tmp]
                    for user in users:
                        senddata(user, recevier_server_smtp, recevier_user, recevier_pwd, content["content"], Subject_tmp,action_id)
                if slack_status == 1:
                    # print("------------------------------------------")
                    # print(content)
                    senddata_slack(slack_webhook,content["content"], Subject,action_id,slack_webhook_index)
                if weixin_status == 1:
                    senddata_weixin(weixin_webhook, content["content"], Subject, action_id, weixin_webhook_index)
        #发送恢复信息
        messagelist=compressnormal(normal)["messagelist"]
        Subject=compressproblem(normal)["Subject"]

        print("----------------------%s发送异常已恢复信息---------------------------" %(action_id))
        print(Subject)
        if len(Subject) == 0:
            Subject = "服务器异常已恢复"
        print("----------------messagelist-------------------------")
        print(messagelist)
        if len(messagelist) != 0:
            for content in  messagelist:
                # print(sendtime)
                Subject_tmp = content["title"]
                if email_status == 1:
                    user_tmp = 'send_to_users_%s' % (user_index)
                    users = locals()[user_tmp]
                    for user in users:
                        senddata(user, recevier_server_smtp, recevier_user, recevier_pwd, content["content"], Subject_tmp,action_id)
                if slack_status == 1:
                    # print("------------------------------------------")
                    # print(content)
                    senddata_slack(slack_webhook,content["content"], Subject,action_id,slack_webhook_index)
                if weixin_status == 1:
                    senddata_weixin(weixin_webhook, content["content"], Subject, action_id, weixin_webhook_index)
    else:
        pass